package com.hhit.collaborative_learning.community;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.hhit.collaborative_learning.dao.BBS_ClassDao;
import com.hhit.collaborative_learning.dao.BBS_SpecialDao;
import com.hhit.collaborative_learning.dao.impl.BBS_ClassDaoImpl;
import com.hhit.collaborative_learning.dao.impl.BBS_SpecialDaoImpl;
import com.hhit.collaborative_learning.utils.DBAccess;
import com.hhit.collaborative_learning.utils.ResponseUtil;
import com.mysql.jdbc.StringUtils;

/**
 * Servlet implementation class DeletZtServlet
 */
@WebServlet("/community/DeletZtServlet")
public class DeletZtServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public DeletZtServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub tyle
		String id = request.getParameter("id");
		String tyle = request.getParameter("tyle");
		Connection con = DBAccess.getInstance().getConn();
		JSONObject jobj = new JSONObject();
		switch (tyle) {
		case "0":
			System.out.println("类型  "+tyle+" 删除  Special");
			jobj=deleteSpecial(con,id);
			break;
		case "1":
			System.out.println("类型  "+tyle+" 删除  Class");
			jobj=deleteClass(con,id);
			break;
		default:
			System.out.println("非法类型  "+tyle);
			break;
		}
		try {
			ResponseUtil.write(response, jobj.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			System.out.println("json  "+jobj.toString());
			DBAccess.getInstance().close(con, null, null);
		}
	}
	private JSONObject deleteClass(Connection con, String id) {
		// TODO Auto-generated method stub
		JSONObject jobj = new JSONObject();
		BBS_ClassDao cd=new BBS_ClassDaoImpl();
		String isSuccess = "false";
		int re = 0;
		if (!StringUtils.isNullOrEmpty(id)) {
			cd.delete(con, " ID="+id);
			if (!StringUtils.isNullOrEmpty(id)) {
				isSuccess = "true";
			}
		}
		jobj.put("result", re);
		jobj.put("isSuccess", isSuccess);
		return jobj;
	}

	private JSONObject deleteSpecial(Connection con, String id) {
		// TODO Auto-generated method stub
		JSONObject jobj = new JSONObject();
		BBS_SpecialDao sd = new BBS_SpecialDaoImpl();
		BBS_ClassDao cd=new BBS_ClassDaoImpl();
		String isSuccess = "false";
		int re = 0;
		if (!StringUtils.isNullOrEmpty(id)) {
			re = sd.delete(con, " ID= " + id);
			cd.delete(con, " specialID="+id);
			if (re != 0) {
				isSuccess = "true";
			}
		}
		jobj.put("result", re);
		jobj.put("isSuccess", isSuccess);
		return jobj;
	}

}
